@pexip/utils
Classes
Interfaces
Type Aliases
AsyncJob
Ƭ AsyncJob: () => Promise
<void
>
Type declaration
▸ (): Promise
<void
>
Returns
Promise
<void
>
Fn
Ƭ Fn<T
, R
>: (...args
: T
) => R
Type parameters
Name | Type |
---|---|
T | extends unknown [] |
R | R |
Type declaration
▸ (...args
): R
Parameters
Name | Type |
---|---|
...args | T |
Returns
R
Variables
ASTERISK
• Const
ASTERISK: "\\*"
Constants
BACKSLASH
• Const
BACKSLASH: "\\\\"
BUILD
• Const
BUILD: "[0-9a-zA-Z-]+"
CARET
• Const
CARET: "\\^"
CLOSE_BRACE
• Const
CLOSE_BRACE: "\\}"
CLOSE_BRACKET
• Const
CLOSE_BRACKET: "\\]"
CLOSE_PARENTHESE
• Const
CLOSE_PARENTHESE: "\\)"
DOLLAR
• Const
DOLLAR: "\\$"
DOT
• Const
DOT: "\\."
NONNUMERIC
• Const
NONNUMERIC: "\\d*[a-zA-Z-][a-zA-Z0-9-]*"
Non-numeric Identifier
Zero or more digits, followed by a letter or hyphen, and then zero or more letters, digits, or hyphens.
NUMERIC
• Const
NUMERIC: "0|[1-9]\\d*"
Numeric Identifier
A single 0
, or a non-zero digit followed by zero or more digits.
NUMERICLOSE
• Const
NUMERICLOSE: "[0-9]+"
Numeric Lose Identifier
At least 1 digit.
OPEN_BRACE
• Const
OPEN_BRACE: "\\{"
OPEN_BRACKET
• Const
OPEN_BRACKET: "\\["
OPEN_PARENTHESE
• Const
OPEN_PARENTHESE: "\\("
PIPE
• Const
PIPE: "\\|"
PLUS
• Const
PLUS: "\\+"
QUESTION_MARK
• Const
QUESTION_MARK: "\\?"
QUEUE_DELAY_IN_MS
• Const
QUEUE_DELAY_IN_MS: 0
QUEUE_DROP_LAST
• Const
QUEUE_DROP_LAST: false
QUEUE_SIZE
• Const
QUEUE_SIZE: 5
QUEUE_THROTTLE_IN_MS
• Const
QUEUE_THROTTLE_IN_MS: 100
Functions
alt
▸ alt(...patternOrCombinators
): Combinator
A Combinator to make the provided patterns as a combination of alternatives
Parameters
Name | Type | Description |
---|---|---|
...patternOrCombinators | PatternCombinator [] | The combination of alternatives, e.g. a\|b\|c |
Returns
Combinator
any
▸ any(patternOrCombinator
): Combinator
A Combinator to make the provided pattern's occurrence as any or more, i.e. *
Parameters
Name | Type | Description |
---|---|---|
patternOrCombinator | PatternCombinator | The pattern to apply |
Returns
Combinator
begin
▸ begin(patternOrCombinator
): Combinator
A Combinator for the match start of a string.
Parameters
Name | Type |
---|---|
patternOrCombinator | PatternCombinator |
Returns
Combinator
cancellablePromise
▸ cancellablePromise<A
, R
>(callback
, cleanup?
): readonly [(...args
: A
) => Promise
<void
| Awaited
<R
>>, () => void
]
Wrapping provided async callback to allow to resolve the async operation with cleanup when the cancel function is called
Type parameters
Name | Type |
---|---|
A | extends unknown [] |
R | R |
Parameters
Name | Type | Description |
---|---|---|
callback | AsyncCallback <A , R > | An async callback to execute |
cleanup? | Cleanup <R > | A cleanup callback to execute when the provided async callback is canceled |
Returns
readonly [(...args
: A
) => Promise
<void
| Awaited
<R
>>, () => void
]
createAsyncQueue
▸ createAsyncQueue(«destructured»?
): Object
Parameters
Name | Type |
---|---|
«destructured» | AsyncQueueOptions |
Returns
Object
Name | Type |
---|---|
enqueue | (job : AsyncJob , runImmediately : boolean ) => void |
execute | () => Promise <void > |
get busy() | boolean |
createQueue
▸ createQueue<T
>(maxSize?
, initial?
, dropLast?
): Queue
<T
>
Type parameters
Name |
---|
T |
Parameters
Name | Type | Default value |
---|---|---|
maxSize | number | QUEUE_SIZE |
initial | T [] | undefined |
dropLast | boolean | QUEUE_DROP_LAST |
Returns
Queue
<T
>
createTokenizer
▸ createTokenizer<T
>(): Tokenizer
<null
| Matched
<T
>>
Type parameters
Name | Type |
---|---|
T | extends Record <string , unknown > |
Returns
Tokenizer
<null
| Matched
<T
>>
debounce
▸ debounce<T
, R
>(fn
, waitDuration
): (this
: unknown
, ...args
: T
) => Promise
<R
>
An higher-order-function that debounce a given function to only invoke after N
amount of time passes since its last call.
Note: Can be extended in the future to allow immediate
(leading) flag and cancel
method options.
Type parameters
Name | Type |
---|---|
T | extends unknown [] |
R | R |
Parameters
Name | Type | Description |
---|---|---|
fn | Fn <T , R > | The function to throttle. |
waitDuration | number | The number of milliseconds to wait before invoke. |
Returns
fn
▸ (this
, ...args
): Promise
<R
>
Parameters
Name | Type |
---|---|
this | unknown |
...args | T |
Returns
Promise
<R
>
Name | Type |
---|---|
cancel | () => void |
end
▸ end(patternOrCombinator
): Combinator
A Combinator for the match end of a string.
Parameters
Name | Type |
---|---|
patternOrCombinator | PatternCombinator |
Returns
Combinator
endsWithAnyOf
▸ endsWithAnyOf(subject
, possibleMatches
): boolean
Parameters
Name | Type |
---|---|
subject | string |
possibleMatches | string [] |
Returns
boolean
firstOr
▸ firstOr<T
>(list
, defaultValue
): T
Selects the first element in a list
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
list | T [] |
defaultValue | T |
Returns
T
T - if list[index] === undefined, it returns defaultValue
getBaseURI
▸ getBaseURI(node?
): string
Parameters
Name | Type | Default value |
---|---|---|
node | Object | document |
node.baseURI | string | undefined |
Returns
string
getTimeLeft
▸ getTimeLeft(futureTimestampSeconds
, earlierTimestampSeconds
): string
Parameters
Name | Type |
---|---|
futureTimestampSeconds | number |
earlierTimestampSeconds | number |
Returns
string
group
▸ group(patternOrCombinator
, name?
, valueType?
): Combinator
A Combinator to group pattern. When name
is provided, it will be one of the
keys that returned from
See
- Parser. This will be the main function to extract information for parsing
- ValueType
Default Value
'string'
Parameters
Name | Type | Description |
---|---|---|
patternOrCombinator | PatternCombinator | The pattern to group |
name? | string | The name for the capturing. when no provided, there is no capturing |
valueType? | ValueType | The type that the capture should be. |
Returns
Combinator
hasOwn
▸ hasOwn<T
, K
>(obj
, prop
): obj is T & Record<K, unknown>
A type utility function to check if the provided object has the prop, and provide a proper type to workaround Typescript's limitation
Type parameters
Name | Type |
---|---|
T | extends object |
K | extends PropertyKey |
Parameters
Name | Type | Description |
---|---|---|
obj | T | The object to check |
prop | K | The prop to be used for the checking |
Returns
obj is T & Record<K, unknown>
identity
▸ identity<T
>(x
): T
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
x | T |
Returns
T
isDefined
▸ isDefined<T
>(t?
): Object
Test that a value t
is set to something and return it, throws on undefined.
This is handy when selecting using an index to eliminiate the infered undefined type.
Example
console.log(isDefined('')); // =\> ''
console.log(isDefined(0)); // =\> 0
console.log(isDefined(false)); // =\> false
console.log(isDefined(null)); // =\> null
console.log(isDefined(undefined)); // will throw
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
t? | T |
Returns
Object
isEmpty
▸ isEmpty<T
, K
>(obj
): obj is undefined | EmptyArray<T> | Record<K, never>
Verify if the provided unknown object is empty
Check the test cases for the assumptions
Type parameters
Name | Type |
---|---|
T | T |
K | extends string | number | symbol |
Parameters
Name | Type | Description |
---|---|---|
obj | undefined | Partial <Record <K , unknown >> | T [] | An object to be tested |
Returns
obj is undefined | EmptyArray<T> | Record<K, never>
join
▸ join(...patternOrCombinators
): Combinator
A Combinator to join the provided patterns as a single pattern
Parameters
Name | Type | Description |
---|---|---|
...patternOrCombinators | PatternCombinator [] | The patterns to join. |
Returns
Combinator
lastOr
▸ lastOr<T
>(list
, defaultValue
): T
Selects the last element in a list
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
list | T [] |
defaultValue | T |
Returns
T
T - if list[index] === undefined, it returns defaultValue
noop
▸ noop(): undefined
A no-operation function.
Example
const object = { 'name': 'fred' };
noop(object) === undefined;
// => true
Returns
undefined
nthOr
▸ nthOr<T
>(list
, index
, defaultValue
): T
Inspired by lodash nth
this will select the given index from an array.
As with the lodash version of nth it will select from the end of the array given a negative index.
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
list | T [] |
index | number |
defaultValue | T |
Returns
T
T - if list[index] === undefined, it returns defaultValue
nthThrows
▸ nthThrows<T
>(list
, index
): Object
Inspired by lodash nth
this will select the given index from an array, but throw if the value is undefined.
As with the lodash version of nth it will select from the end of the array given a negative index.
This is likely to be most useful in tests. You might want nthOr, firstOr, or lastOr instead
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
list | T [] |
index | number |
Returns
Object
option
▸ option(patternOrCombinator
): Combinator
A Combinator to make the provided pattern's occurrence as optional. i.e. ?
Parameters
Name | Type | Description |
---|---|---|
patternOrCombinator | PatternCombinator | The pattern to apply |
Returns
Combinator
pipe
▸ pipe<T
, R
>(fn1
, ...fns
): (...args
: T
) => R
Pipes the value of an expression into a pipeline of functions.
Example
const len = (s: string): number => s.length;
const double = (n: number): number => n * 2;
// Without pipe
const doubleLen = (s: string) => double(len(s));
// With pipe
const doubleLen = pipe(len, double);
Type parameters
Name | Type |
---|---|
T | extends unknown [] |
R | R |
Parameters
Name | Type |
---|---|
fn1 | Fn <T , R > |
...fns | Rns <R > |
Returns
fn
▸ (...args
): R
Parameters
Name | Type |
---|---|
...args | T |
Returns
R
prig
▸ prig(baseIndex
, shuffleID
, listSize
): number
Pseudorandom index generator implemented with Miller Shuffle Algorithm D variant NEW April 2023 aka: MillerShuffleAlgo_d
It produces a shuffled Index given a base Index, a shuffle ID "seed" and the length of the list being indexed. For each inx: 0 to listSize-1, unique indexes are returned in a pseudo "random" order. Utilizes minimum resources. As such there is no better choice for a playlist shuffle.
See
- https://en.wikipedia.org/wiki/Pseudorandom_index_generator
- https://github.com/RondeSC/Miller_Shuffle_Algo
Parameters
Name | Type | Description |
---|---|---|
baseIndex | number | The index to be shuffled |
shuffleID | number | The seed number |
listSize | number | The size of the list |
Returns
number
shuffled index
range
▸ range(patternOrCombinator
, min
, max?
): Combinator
A Combinator to make the provided pattern's occurrence as in specified range.
i.e. {min,max}
Parameters
Name | Type | Description |
---|---|---|
patternOrCombinator | PatternCombinator | The pattern to apply |
min | number | The minimum occurrence |
max? | number | The Maximum occurrence, undefined means unlimited |
Returns
Combinator
subscribeEvent
▸ subscribeEvent<T
>(target
, type
, handler
, options?
): UnsubscribeFn
Subscribe an EventTarget's event and return a unsubscribe function
Type parameters
Name | Type |
---|---|
T | extends EventTarget |
Parameters
Name | Type | Description |
---|---|---|
target | T | An EventTarget to be subscribed |
type | Params <T ["addEventListener" ]>[0 ] | The type to pass into addEventListener function |
handler | Params <T ["addEventListener" ]>[1 ] | The handle to pass into addEventListener function |
options? | Params <T ["addEventListener" ]>[2 ] | The options to pass into addEventListener function |
Returns
UnsubscribeFn
throttle
▸ throttle<T
, R
>(fn
, wait?
): (...args
: T
) => void
Creates a throttled function that only invokes func at most once per every wait milliseconds
Type parameters
Name | Type |
---|---|
T | extends unknown [] |
R | R |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
fn | Fn <T , R > | undefined | The function to throttle. |
wait | number | 0 | The number of milliseconds to throttle invocations to. |
Returns
fn
▸ (...args
): void
Parameters
Name | Type |
---|---|
...args | T |
Returns
void